﻿using NLog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 完工订单定时任务
{
    public partial class Form检查MYSQL服务: Form
    {
        int 毫秒 = 60000;
        //日志
        NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

        public Form检查MYSQL服务()
        {
            InitializeComponent();
        }

        private void Form检查MYSQL服务_Load(object sender, EventArgs e)
        {

        }
        //检查MYSQL服务
        private void 检查MYSQL服务()
        {
            //检查MYSQL服务
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.Arguments = "/c sc query mysql";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.CreateNoWindow = true;
            process.Start();
            string output = process.StandardOutput.ReadToEnd();
            process.WaitForExit();
            if (output.Contains("RUNNING"))
            {
                logger.Info("MYSQL服务正在运行");
                textBox1.Text = DateTime.Now.ToString("yyyyy-MM-dd HH:mm:ss")+"——————"+("MYSQL服务正在运行") + textBox1.Text+"\r\n";
            }
            else
            {
                logger.Info("MYSQL服务没有运行");
                textBox1.Text = DateTime.Now.ToString("yyyyy-MM-dd HH:mm:ss") + "——————" + ("MYSQL服务没有运行") + textBox1.Text + "\r\n";
                for (int i = 0; i < 3; i++)
                {
                    if (启动MYSQL服务(i))
                    {
                        break;
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(毫秒);
                    }
                }

            }
            //如果textBox1超过20行，只保留前20行
            if (textBox1.Lines.Length > 20)
            {
                string[] lines = textBox1.Lines.Take(20).ToArray();
                textBox1.Text = string.Join("\r\n", lines);
            }
        }
        //启动MYSQL服务
        private bool 启动MYSQL服务(int 次数)
        {
            bool 重新启动成功 = true;
            //启动MYSQL服务
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.Arguments = "/c net start mysql";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.CreateNoWindow = true;
            process.Start();
            string output = process.StandardOutput.ReadToEnd();
            process.WaitForExit();
            if (output.Contains("成功"))
            {
                logger.Info($"第{次数}次：MYSQL服务启动成功");
                textBox1.Text = DateTime.Now.ToString("yyyyy-MM-dd HH:mm:ss") + "——————"+ $"第{次数}次：MYSQL服务启动成功" + textBox1.Text + "\r\n";
            }
            else
            {
                重新启动成功 = false;
                logger.Info($"第{次数}次：MYSQL服务启动失败");
                textBox1.Text = DateTime.Now.ToString("yyyyy-MM-dd HH:mm:ss") + "——————" + $"第{次数}次：MYSQL服务启动失败"+ textBox1.Text + "\r\n";
            }

            return 重新启动成功;
        }

        private void button开始_Click(object sender, EventArgs e)
        {
            //如果输入的不是数字就提示
            if (!int.TryParse(textBox2.Text.Trim(), out int 毫秒))
            {
                MessageBox.Show("请输入数字");
                return;
            }
            else
            {
                毫秒 = Convert.ToInt32(textBox2.Text.Trim())*60000;
                timer1.Interval = 毫秒;
                textBox1.Text = DateTime.Now.ToString("yyyyy-MM-dd HH:mm:ss") + "——————" + ($"定时器开始运行,间隔时间{timer1.Interval/ 60000}分钟") + textBox1.Text + "\r\n";
                timer1.Enabled = true;
                timer1.Start();

            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            检查MYSQL服务();
        }
    }
}
